package com.youjiang.meeting.utils.weixin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.http.ParseException;

import com.google.gson.Gson;
import com.youjiang.meeting.service.impl.WeiXinMassegeServiceImpl;

import net.sf.json.JSONObject;

public class CodeTogetTokenService {
	private static String TOKENURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
	private static String USERINFOURL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
	private static Gson gson = new Gson();

	
//	public static Map<String , String> getInetToken(String code) {
//		String url = TOKENURL.replace("CODE", code).replace("APPID", WeiXinMassegeServiceImpl.config.getAppid()+"").replace("SECRET",
//				WeiXinMassegeServiceImpl.config.getApsecrect());
//		String data = null;
//		Map<String, String> map=new HashMap<String, String>();
//		try {
//			JSONObject postStr = WeixinUtil.doGetStr(url);
//			/**
//			 * { "access_token":"ACCESS_TOKEN", "expires_in":7200,
//			 * "refresh_token":"REFRESH_TOKEN", "openid":"OPENID",
//			 * "scope":"SCOPE", "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }
//			 */
//			String token = postStr.optString("access_token", null);
//			int expires = postStr.optInt("expires_in");
//			String refresh = postStr.optString("refresh_token");
//			String openid = postStr.optString("openid");
//			String scope = postStr.optString("scope");
//			String unionid = postStr.optString("unionid");
//			Iterator postKeys = postStr.keys();
//			while(postKeys.hasNext()){
//				String postKey=(String) postKeys.next();
//				map.put(postKey, postStr.optString(postKey));
//			}
//			if (token != null) {
//				String infoUrl = USERINFOURL.replace("ACCESS_TOKEN", token).replace("OPENID", openid);
//				JSONObject userinfo = WeixinUtil.doGetStr(infoUrl);
//				Iterator keys = userinfo.keys();
//				while(keys.hasNext()){
//					String key=(String) keys.next();
//					map.put(key, userinfo.optString(key));
//				}
//			} else {
//				data = postStr.toString();
//			}
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return map;
//	}

//	public static String getInetOpenid(String code) throws ParseException, IOException {
//		String openid = null;
//		String url = TOKENURL.replace("CODE", code).replace("APPID", WeiXinMassegeServiceImpl.config.getAppid()+"").replace("SECRET",
//				WeiXinMassegeServiceImpl.config.getApsecrect());
//		JSONObject postStr = WeixinUtil.doGetStr(url);
//		openid = postStr.optString("openid",null);
//		return openid;
//	}
}
